package com.qf.service.impl;

import com.qf.enums.AppHttpCodeEnum;
import com.qf.exception.SystemException;
import com.qf.result.ResponseResult;
import com.qf.service.UploadService;
import com.qf.utils.PathUtils;
import com.qf.utils.QiNuUtils;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Service
@Data
public class UploadServiceImpl implements UploadService {

    @Resource
    private QiNuUtils qiNuUtils;

    @Override
    public ResponseResult uploadImg(MultipartFile img) {

        //判断文件类型
        //获取原始文件名
        String originalFilename = img.getOriginalFilename();
        //对原始文件名进行判断
        if (!originalFilename.endsWith(".png") && !originalFilename.endsWith(".jpg")) {
            throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
        }
        //如果判断通过上传文件到OSS
        String filePath = PathUtils.generateFilePath(originalFilename);
        String url = qiNuUtils.uploadOss(img, filePath);
        return ResponseResult.okResult(url);
    }
}